﻿using System.Reflection;

namespace Borg
{
    internal class SlotPlugTask : Task<SlotInfo>
    {
        private readonly PlugInfo _pi;
        private readonly PlugTypeInfo _pti;

        private readonly bool _isManual;

        public override string Name
        {
            get
            {
                return _pti != null
                           ? string.Format("Plug({0}, {1})", obj.Name, _pti.Name)
                           : (_pi == null
                                  ? string.Format("Plug({0})", obj.Name)
                                  : string.Format("Plug({0}, {1})", obj.Name, _pi.Name));
            }
        }

        public SlotPlugTask(SlotInfo obj, PlugInfo pi, PlugTypeInfo pti,
            bool isManual, uint taskGroupId, Assembly initiator)
            : base(obj, taskGroupId, initiator)
        {
            _pi = pi;
            _pti = pti;
            _isManual = isManual;
        }

        public override void Execute()
        {
            if (_pti != null)
                obj.InternalPlug(_pti, _isManual, taskGroupId, initiator);
            else if (_pi != null)
                obj.InternalPlug(_pi, _isManual, taskGroupId, initiator, true);
            else
                obj.InternalPlug(taskGroupId, initiator);
        }

        public override string ToString()
        {
            if (_pti != null)
            {
                return string.Format("{0}: {1}.InternalPlug({2}, {3})",
                    (object)taskGroupId, (object)obj, (object)_pti, (object)(_isManual ? 1 : 0));
            }

            var str =
                _pi == null
                    ? string.Format("{0}: {1}.InternalPlug()", taskGroupId, obj)
                    : string.Format("{0}: {1}.InternalPlug({2}, {3})",
                        (object)taskGroupId, (object)obj, (object)_pi, (object)(_isManual ? 1 : 0));

            return str;
        }
    }
}

